package com.amita.ez.base;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;

@Aspect
@Component
@Order(-1)
@Slf4j
public class SysLogAop {



    @Pointcut("execution(public * com.amita.ez.web..*.*(..))")
    public void webLog() {
    }


    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) {
// 接收到请求，记录请求内容

//        log.info("WebLogAspect.doBefore()");
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();


        // 记录下请求内容

        System.out.println(request.getMethod() + " => URL : " + request.getRequestURL().toString());
        log.info(request.getMethod() + " => URL : " + request.getRequestURL().toString());
        log.info("IP : " + request.getRemoteAddr());
        log.info("CLASS_METHOD : " + joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
        log.info("ARGS : " + Arrays.toString(joinPoint.getArgs()));

        System.out.println("========params========");

        Map<String, String[]> map = request.getParameterMap();
        map.forEach((k, v) -> {

            System.out.println(String.format("[%s] = %s", k, Arrays.toString(v)));
        });



    }



}
